package com.hzit.pay.center.customer;

import com.hzit.pay.center.config.MqConfig;
import com.hzit.pay.center.model.NoticeInfo;
import com.hzit.pay.center.service.INoticeRuleService;
import com.hzit.pay.center.service.INoticeService;
import com.pay.center.common.req.PayCallBackData;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

/**
 * 异步通知消费者
 */
@Component
public class PayCallBackCustomer {


    private Logger logger = LoggerFactory.getLogger(PayCallBackCustomer.class);

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private INoticeService noticeService;

    @Autowired
    private INoticeRuleService noticeRuleService;

    @RabbitListener(queues = MqConfig.BUSINESS_QUEUEA_NAME)
    public void excute(PayCallBackData callBackData, Channel channel, Message message){
        logger.info("#####异步通知消费者开始消费消息：{}#####。",callBackData);

        NoticeInfo noticeInfo = noticeService.queryBySerialNo(callBackData.getPaySerialNo());

        if (ObjectUtils.isEmpty(noticeInfo)){
            return;
        }

        if (noticeInfo.getNotifyCount()>=8){
            return;
        }

        //再次发送. 延时队列中。
        String url = "http://127.0.0.1:8899/orderCenter/callBack";
        String result = restTemplate.postForObject(url,callBackData,String.class);

        if ("success".equalsIgnoreCase(result)){
            //发送成功，更新通知记录为成功
            try {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

                //更新通知次数
                NoticeInfo record = new NoticeInfo();
                record.setId(noticeInfo.getId());
                record.setNotifyStatus("1");
                record.setNotifyCount(noticeInfo.getNotifyCount()+1);
                noticeService.updateNotice(record);

            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }else{
            //通知失败，加一次，
            NoticeInfo record = new NoticeInfo();
            record.setId(noticeInfo.getId());
            record.setNotifyStatus("0");
            record.setNotifyCount(noticeInfo.getNotifyCount()+1);
            noticeService.updateNotice(record);

            //设置消息的延迟
            Long ttl = noticeRuleService.getTtl(String.valueOf(noticeInfo.getNotifyCount()));
            MessagePostProcessor messagePostProcessor = new MyMessagePostProcessor(ttl);
            rabbitTemplate.convertAndSend(MqConfig.BUSINESS_EXCHANGE_NAME,"",callBackData,messagePostProcessor);
        }



    }



}
